<template>
  <div>
    <!--头部操作栏-->
    <el-form
      :inline="true"
      size="small">
      <el-form-item v-if="auth.add">
        <el-button
          icon="el-icon-plus"
          :disabled="loading"
          @click="handleCreate">新增</el-button>
      </el-form-item>

      <el-form-item>
        <el-button-group>
          <el-button
            v-if="auth.enable"
            icon="el-icon-check"
            :disabled="loading"
            @click="handleState(1)">启用</el-button>

          <el-button
            v-if="auth.disable"
            icon="el-icon-close"
            :disabled="loading"
            @click="handleState(0)">禁用</el-button>
        </el-button-group>
      </el-form-item>

      <el-form-item v-if="auth.del">
        <el-button
          icon="el-icon-delete"
          :disabled="loading"
          @click="handleDelete(multipleSelection)">删除</el-button>
      </el-form-item>
    </el-form>

    <!--数据列表-->
    <el-table
      v-loading="loading"
      :data="currentTableData"
      :highlight-current-row="true"
      @selection-change="handleSelectionChange"
      @sort-change="sortChange">
      <el-table-column type="selection" width="50">
      </el-table-column>
      <el-table-column
        label="标题"
        prop="title"
        :show-overflow-tooltip="true">
      </el-table-column>

      <el-table-column
        label="文章来源"
        v-if="false"
        prop="articleSource"
        :show-overflow-tooltip="true">
      </el-table-column>

      <el-table-column
        label="资讯类型"
        :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{newsTypeMap[scope.row.type]}}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="点赞数量"
        prop="likeCount"
        :show-overflow-tooltip="true">
      </el-table-column>

      <el-table-column
        label="分享数量"
        prop="shareCount"
        :show-overflow-tooltip="true">
      </el-table-column>

      <el-table-column
        label="评论数量"
        prop="commentCount"
        :show-overflow-tooltip="true">
      </el-table-column>

      <el-table-column
        label="访客数量"
        prop="visitorCount"
        :show-overflow-tooltip="true">
      </el-table-column>

      <el-table-column
        label="创建时间"
        prop="createTime"
        :show-overflow-tooltip="true">
      </el-table-column>

      <el-table-column
        label="状态"
        prop="status"
        sortable="custom"
        align="center"
        width="100">
        <template slot-scope="scope">
          <el-tag
            size="mini"
            :type="statusMap[scope.row.status].type"
            style="cursor: pointer;"
            effect="light"
            @click.native="handleStatus(scope.$index)">
            {{statusMap[scope.row.status].text}}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        min-width="140">
        <template slot-scope="scope">
          <el-button
            v-if="auth.set"
            size="small"
            @click="handleUpdate(scope.$index)"
            type="text">编辑</el-button>

          <el-button
            v-if="auth.del"
            size="small"
            @click="handleDelete(scope.$index)"
            type="text">删除</el-button>

            <el-dropdown
            :show-timeout="50"
            size="small">
            <el-button
              class="d2-ml-10"
              size="small"
              type="text">更多操作</el-button>

            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                @click.native="handleLookDetail(scope.row.newsId)">
                资讯详情
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <!--新增数据模态框-->
    <el-dialog
      :title="textMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      :append-to-body="true"
      :close-on-click-modal="false"
      width="600px">
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        label-width="80px">
        <el-form-item
          label="账号"
          prop="username">
          <el-input
            v-model="form.username"
            :disabled="dialogStatus !== 'create'"
            placeholder="请输入账号"
            :clearable="true"/>
        </el-form-item>

        <el-form-item
          v-if="dialogStatus === 'create'"
          label="密码"
          prop="password">
          <el-input
            type="password"
            v-model="form.password"
            placeholder="请输入密码"
            :clearable="true"/>
        </el-form-item>

        <el-form-item
          v-if="dialogStatus === 'create'"
          label="确认密码"
          prop="password_confirm">
          <el-input
            type="password"
            v-model="form.password_confirm"
            placeholder="请再次输入密码"
            :clearable="true"/>
        </el-form-item>

        <el-form-item
          label="昵称"
          prop="nickname">
          <el-input
            v-model="form.nickname"
            placeholder="请输入昵称"
            :clearable="true"/>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button
          @click="dialogFormVisible = false"
          size="small">取消</el-button>

        <el-button
          v-if="dialogStatus === 'create'"
          type="primary"
          :loading="dialogLoading"
          @click="create"
          size="small">确定</el-button>

        <el-button
          v-else type="primary"
          :loading="dialogLoading"
          @click="update(form.index)"
          size="small">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    tableData: {
      default: () => []
    },
    loading: {
      default: false
    },
    group: {
      default: () => []
    }
  },
  data () {
    return {
      currentTableData: [],
      multipleSelection: [],
      auth: {
        add: true,
        del: true,
        set: true,
        enable: true,
        disable: true,
        reset: false
      },
      dialogLoading: false,
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      newsTypeMap: {
        1: '图文新闻资讯',
        2: '视频新闻资讯'
      },
      statusMap: {
        1: {
          text: '开发',
          type: 'danger'
        },
        2: {
          text: '关闭',
          type: 'success'
        }
      },
      form: {
        index: undefined,
        client_id: undefined,
        username: undefined,
        password: undefined,
        password_confirm: undefined,
        group_id: undefined,
        nickname: undefined
      },
      rules: {
        username: [
          {
            required: true,
            message: '账号不能为空',
            trigger: 'blur'
          },
          {
            min: 4,
            max: 20,
            message: '长度在 4 到 20 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  watch: {
    tableData: {
      handler (val) {
        this.currentTableData = val
      },
      immediate: true
    }
  },
  mounted () {
    // this._validationAuth()
  },
  methods: {
    // 获取列表中的用户编号
    _getClientIdList (val) {
      let clients = []
      if (Array.isArray(val)) {
        val.forEach(value => {
          clients.push(value.admin_id)
        })
      } else {
        clients.push(this.currentTableData[val].admin_id)
      }

      return clients
    },
    // 选中数据项
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    // 获取排序字段
    sortChange ({ column, prop, order }) {
      let sort = {
        order_type: undefined,
        order_field: undefined
      }

      if (column && order) {
        sort.order_type = order === 'ascending' ? 'asc' : 'desc'
        sort.order_field = prop
      }

      this.$emit('sort', sort)
    },
    // 批量设置状态
    handleState (state) {
      let clients = this._getClientIdList(this.multipleSelection)
      if (clients.length === 0) {
        this.$message.error('请选择要操作的数据')
        return
      }

      this.$confirm('确定要执行该操作吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        closeOnClickModal: false
      })
        .then(() => {
        })
        .catch(() => {
        })
    },
    // 批量删除
    handleDelete (val) {
      let clients = this._getClientIdList(val)
      if (clients.length === 0) {
        this.$message.error('请选择要操作的数据')
        return
      }

      this.$confirm('确定要执行该操作吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        closeOnClickModal: false
      })
        .then(() => {
        })
        .catch(() => {
        })
    },
    // 弹出新建对话框
    handleCreate () {
      this.$router.push({ name: 'information-news-create', params: { } })
    },
    // 请求新建用户
    create () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.dialogLoading = true
        }
      })
    },
    // 弹出编辑对话框
    handleUpdate (index) {
      this.form = {
        index: index,
        client_id: this.currentTableData[index].admin_id,
        username: this.currentTableData[index].username,
        group_id: this.currentTableData[index].group_id,
        nickname: this.currentTableData[index].nickname
      }

      // 处理el-select项不存在的bug
      if (!this.group.find(item => item.group_id === this.form.group_id)) {
        this.form.group_id = undefined
      }

      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })

      this.dialogStatus = 'update'
      this.dialogLoading = false
      this.dialogFormVisible = true
    },
    // 请求修改用户
    update (index) {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.dialogLoading = true
        }
      })
    },

    handleLookDetail (val) {
      this.$router.push({ name: 'information-news-create', params: { 'newsId': val } })
    },

    // 切换状态
    switchStatus (index) {
      let oldData = this.currentTableData[index]
      const newStatus = oldData.status ? 0 : 1

      if (oldData.status > 1) {
        return
      }

      // 禁用权限检测
      if (newStatus === 0 && !this.auth.disable) {
        return
      }

      // 启用权限检测
      if (newStatus === 1 && !this.auth.enable) {
        return
      }

      this.$set(this.currentTableData, index, { ...oldData, status: 2 })
    }
  }
}
</script>
